package com.xishu.util;

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * 将HTML文件转成PDF文件
 */
public class PdfUtil {
    private static Logger logger = LoggerFactory.getLogger(PdfUtil.class);
    private static final String DEST = "target/HelloWorld.pdf";

    public static void main(String[] args) throws Exception {
        File file = getDolaFile();

//        File file = getSampleFile();


        System.out.println("file.getAbsolutePath() = " + file.getAbsolutePath());
    }

    private static File getSampleFile() throws Exception {
        return htmlToPdf(
                "<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>" +
                        "<head>" +
                        "  <meta charset='UTF-8'/>" +
                        "  <title>表格样式</title>" +
                        "  <meta name='viewport' content='initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width'/>" +
                        "  <link rel='stylesheet' href='https://static.loyalvalleycapital.com/web/css/frame.css'/>" +
                        "  <style>" +
                        "  table.table-separate th{" +
                        "    font-weight:bold;" +
                        "    font-size:14px;" +
                        "    border-top:1px solid #F3EDE9 !important;" +
                        "  }" +
                        "  table.table-separate td{" +
                        "    padding: 13px 0;" +
                        "    font-weight:100;" +
                        "  }" +
                        "  .table-separate td.tit{" +
                        "    background-color: #f4f9fe;" +
                        "    font-weight:normal;" +
                        "    padding:22px 0;" +
                        "    width:15%;" +
                        "  }" +
                        "  .table-separate td.cont{" +
                        "    text-align: left;" +
                        "    padding:16px 22px;" +
                        "    width:85%;" +
                        "    line-height:175%;" +
                        "  }" +
                        "  .table-separate.no-border th{" +
                        "    border:none;" +
                        "    text-align: left;" +
                        "  }" +
                        "  .table-separate.no-border td{" +
                        "    text-align: left;" +
                        "    border:none;" +
                        "  }" +
                        " " +
                        "table {" +
                        "border-collapse: collapse;" +
                        "table-layout: fixed;" +
                        "word-break:break-all;" +
                        "font-size: 10px;" +
                        "width: 100%;" +
                        "text-align: center;" +
                        "}" +
                        "td {" +
                        "word-break:break-all;" +
                        "word-wrap : break-word;" +
                        "}" +
                        "@page {" +
                        "size:210mm 297mm;//纸张大小A4" +
                        "margin: 0.25in;" +
                        "-fs-flow-bottom: 'footer';" +
                        "-fs-flow-left: 'left';" +
                        "-fs-flow-right: 'right';" +
                        "padding: 1em;" +
                        "}" +
                        "#footer {" +
                        "font-size: 90%; font-style: italic;" +
                        "position: absolute; top: 0; left: 0;" +
                        "-fs-move-to-flow: 'footer';" +
                        "}" +
                        "#pagenumber:before {" +
                        "content: counter(page);" +
                        "}" +
                        "#pagecount:before {content: counter(pages);" +
                        "}" +
                        "</style>" +
                        "</head>" +
                        "<body class='bg-white pb-3'  style = 'font-family: SimSun;'>" +
                        " <div id='footer' style=''>  Page <span id='pagenumber'/> of <span id='pagecount'/> </div>" +
                        "<div style='max-width:600px;margin:0 auto;padding:10px;'>" +
                        "  <div class='f18 text-center mv-2 bold'>合伙人服务日报</div>" +
                        "  <div class='f14 text-center mb-3'>2017年8月28日</div>" +
                        "" +
                        "  <div class=''>" +
                        "    <div class='f14 mb bold'>一、旗舰基金股东动态</div>" +
                        "    <div class='f14 mb'>1、旗舰基金公司新闻</div>" +
                        "    <table class='table-separate'>" +
                        "      <tbody>" +
                        "      <tr>" +
                        "        <td class='tit' valign='middle'>刚泰</td>" +
                        "" +
                        "        <td class='cont'>" +
                        "          <p class='bold'>第 3 期员工持股计划（草案）摘要</p>" +
                        "          <p>1、甘肃刚泰控股（集团）股份有限公司（以下简称“公司”）第3期员工持 股计划（以下简称“第3期员工持股计划”）根据《中华人民共和国公司法》、《中华人民共和国证券法》、" +
                        "  《关于上市公司实施员工持股计划试点的指导意见》 及其他有关法律、法规、规范性文件以及《甘肃刚泰控股（集团）股份有限公司章程》的规定制定，遵循公平、公正、公开的原则，旨在完善公司的激励机制，确保公司未来发展战略和经营目标的实现。</p>" +
                        "          <p>4、第3期员工持股计划以“份”作为认购单位，每份份额为1元，起始认购 份数为100万份，超过100万份的，以10万份的整数倍累积计算。公司全部员工 持股计划涉及的股票数量累计不超过公司现有股本总额的10%，任一持有人持有的 员工持股计划份额所对应的标的股票数量不超过公司股本总额的 1%（不包括员工 在公司首次公开发行股票上市前获得的股份、通过二级市场自行购买的股份及通过股权激励获得的股份）。</p>" +
                        "        </td>" +
                        "      </tr>" +
                        "      </tbody>" +
                        "    </table>" +
                        "" +
                        "" +
                        "    <div class='f14 mt-3 mb bold'>一、客户沟通和反馈</div>" +
                        "    <div class='f14 mb'>今天联系的客户：</div>" +
                        "    <div class=''>" +
                        "      <table class='table-separate'>" +
                        "        <thead>" +
                        "        <th>联系人</th>" +
                        "        <th>沟通内容</th>" +
                        "        <th>客户反馈</th>" +
                        "        </thead>" +
                        "        <tbody>" +
                        "        <tr>" +
                        "          <td>XXX</td>" +
                        "          <td>XXX</td>" +
                        "          <td>XXX</td>" +
                        "        </tr>" +
                        "        <tr>" +
                        "          <td>XXX</td>" +
                        "          <td>XXX</td>" +
                        "          <td>XXX</td>" +
                        "        </tr>" +
                        "        </tbody>" +
                        "      </table>" +
                        "    </div>" +
                        "" +
                        "" +
                        "    <div class='f14 mt-3 mb bold'>三、官网、微信和客户服务系统变化</div>" +
                        "    <div class=''>" +
                        "      <table class='table-separate'>" +
                        "        <tbody>" +
                        "        <tr>" +
                        "          <td class='tit' valign='middle'>运营</td>" +
                        "          <td class=''>" +
                        "            <p class='bold'>XXX</p>" +
                        "          </td>" +
                        "          <td class='text-left pl-2'>" +
                        "            <p>· 上海正心公益基金会正式获批 1727 </p>" +
                        "            <p>· XXXX</p>" +
                        "          </td>" +
                        "        </tr>" +
                        "      " +
                        "        </tbody>" +
                        "      </table>" +
                        "    </div>" +
                        "" +
                        "    <div class='f14 mt-3 mb bold'>四、其他工作</div>" +
                        "    <div class='f14 mb'>近期过生日的客户有： </div>" +
                        "    <div class=''>" +
                        "      <p>8.27 王军</p>" +
                        "      <p class='bold'>8.27 陈鱼海</p>" +
                        "    </div>" +
                        "  </div>" +
                        "</div>" +
                        "" +
                        "</body>" +
                        "</html>");
    }

    public static File getDolaFile() throws Exception {
        File file = htmlToPdf("<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>" +
                "<head>" +
                " <meta charset='UTF-8'/>" +
                "  <meta http-equiv='X-UA-Compatible' content='IE=edge'/>" +
                "  <meta name='viewport' content='width=device-width, initial-scale=1.0'/>" +
                "  <style>" +
                "    *{margin: 0;padding: 0;box-sizing: border-box;}" +
                "    html, body{width: 100%;height: 100%;font-size: 20px;}" +
                "    .wrapper{width: 30rem;height: auto;display: flex;flex-direction: column;margin: 0 auto;padding: 1rem;}" +
                "  </style>" +
                "  <title>闭店总报告</title>" +
                "</head>" +
                "<body  style = 'font-family: SimSun;'>  <div class='wrapper'><section style='width: 100%;'>" +
                "      <div style='text-align: center;font-size: 1.5rem;font-weight: bold;'>好又来加盟连锁餐饮公司</div>" +
                "      <div style='text-align: center;margin: 1rem 0;'>（分店名称zh）</div>" +
                "    </section> <section style='width: 100%;'>" +
                "      <div style='display: flex;'>" +
                "        <span style='flex-shrink: 0;'>地址：</span>" +
                "        <span style='flex: 1;'>香港特別行政區 港島 中西區 详细地址-中文</span>" +
                "      </div>" +
                "<div style='display: flex;padding-bottom: 0.5rem;'>" +
                "        <span style='flex-shrink: 0;'>日期：</span>" +
                "        <span style='flex: 1;'>2021/11/13</span>" +
                "      </div>" +
                "    </section>  <section style='width: 100%;'> <table border='1' style='width: 100%;margin-bottom: 1rem;table-layout: fixed;border: none;'> <tbody>" +
                "          <tr style='font-weight:bold'>" +
                "            <td style='border: none;'>支付方式</td>" +
                "            <td style='border: none;text-align: center;'>堂食</td>" +
                "            <td style='border: none;text-align: center;'>外賣</td>" +
                "            <td style='border: none;text-align: center;'>總額</td>" +
                "          </tr> " +
                "<tr>" +
                "            <td style='border: none;'>現金</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "          </tr><tr>" +
                "            <td style='border: none;'>微信</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "          </tr><tr>" +
                "            <td style='border: none;'>支付寶</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "          </tr><tr>" +
                "            <td style='border: none;'>八達通</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "          </tr><tr>" +
                "            <td style='border: none;'>MasterCard</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "          </tr><tr>" +
                "            <td style='border: none;'>Visa</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "          </tr><tr>" +
                "            <td style='border: none;'>銀聯</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "          </tr><tr>" +
                "            <td style='border: none;'>Amex</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "          </tr><tr>" +
                "            <td style='border: none;'>Stripe</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "          </tr> <tr>" +
                "            <td style='border: none;padding-bottom: 0.5rem;'>外賣平臺</td>" +
                "            <td style='border: none;text-align: center;padding-bottom: 0.5rem;'></td>" +
                "            <td style='border: none;text-align: center;padding-bottom: 0.5rem;'></td>" +
                "            <td style='border: none;text-align: center;padding-bottom: 0.5rem;'>0.0</td>" +
                "          </tr> <tr>" +
                "            <td style='border: none;border-top: 1px dashed #000;padding-top: 0.5rem;font-weight: bold;font-size: 1.5rem;padding-bottom: 0.5rem;'>銷售額</td>" +
                "            <td style='border: none;text-align: center;border-top: 1px dashed #000;padding-top: 0.5rem;font-weight: bold;padding-bottom: 0.5rem;'>0.0</td>" +
                "            <td style='border: none;text-align: center;border-top: 1px dashed #000;padding-top: 0.5rem;font-weight: bold;padding-bottom: 0.5rem;'>0.0</td>" +
                "            <td style='border: none;text-align: center;border-top: 1px dashed #000;padding-top: 0.5rem;font-weight: bold;padding-bottom: 0.5rem;'>0.0</td>" +
                "          </tr> <tr>" +
                "            <td style='border: none;border-top: 1px dashed #000;padding-top: 0.5rem;'>服務費</td>" +
                "            <td style='border: none;border-top: 1px dashed #000;padding-top: 0.5rem;'></td>" +
                "            <td style='border: none;border-top: 1px dashed #000;padding-top: 0.5rem;'></td>" +
                "            <td style='border: none;text-align: center;border-top: 1px dashed #000;padding-top: 0.5rem;'>0.0</td>" +
                "          </tr> <tr>" +
                "            <td style='border: none;'>餐盒費</td>" +
                "            <td style='border: none;'></td>" +
                "            <td style='border: none;'></td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "          </tr> <tr>" +
                "            <td style='border: none;'>外賣折扣</td>" +
                "            <td style='border: none;'></td>" +
                "            <td style='border: none;'></td>" +
                "            <td style='border: none;text-align: center;'>-0.0</td>" +
                "          </tr> <tr>" +
                "            <td style='border: none;padding-bottom: 0.5rem;'>優惠券</td>" +
                "            <td style='border: none;padding-bottom: 0.5rem;'></td>" +
                "            <td style='border: none;padding-bottom: 0.5rem;'></td>" +
                "            <td style='border: none;text-align: center;padding-bottom: 0.5rem;'>0.0</td>" +
                "          </tr><tr>" +
                "            <td style='border: none;border-top: 1px dashed #000;padding-top: 0.5rem;'>未支付</td>" +
                "            <td style='border: none;text-align: center;border-top: 1px dashed #000;padding-top: 0.5rem;'>0.0</td>" +
                "            <td style='border: none;text-align: center;border-top: 1px dashed #000;padding-top: 0.5rem;'>0.0</td>" +
                "            <td style='border: none;text-align: center;border-top: 1px dashed #000;padding-top: 0.5rem;'>0.0</td>" +
                "          </tr> <tr>" +
                "            <td style='border: none;'>已退款</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "            <td style='border: none;text-align: center;'>0.0</td>" +
                "          </tr> </tbody></table>" +
                "    </section> <section><div style='display: flex;justify-content: space-between;'>" +
                "        <span>客流量</span>" +
                "        <span style='flex: 1;border-top: 1px dashed #000;height: 0;position: relative;top: 0;bottom: 0;margin: auto 1rem;'></span>" +
                "        <span>0人</span>" +
                "      </div><div style='display: flex;justify-content: space-between;'>" +
                "        <span>訂單量</span>" +
                "        <span style='flex: 1;border-top: 1px dashed #000;height: 0;position: relative;top: 0;bottom: 0;margin: auto 1rem;'></span>" +
                "        <span>0單</span>" +
                "      </div><div style='display: flex;justify-content: space-between;'>" +
                "        <span>刪除訂單</span>" +
                "        <span style='flex: 1;border-top: 1px dashed #000;height: 0;position: relative;top: 0;bottom: 0;margin: auto 1rem;'></span>" +
                "        <span>0單</span>" +
                "      </div><div style='display: flex;justify-content: space-between;'>" +
                "        <span>刪除菜品</span>" +
                "        <span style='flex: 1;border-top: 1px dashed #000;height: 0;position: relative;top: 0;bottom: 0;margin: auto 1rem;'></span>" +
                "        <span>0個</span>" +
                "      </div><div style='display: flex;justify-content: space-between;'>" +
                "        <span>餐廳堂食</span>" +
                "        <span style='flex: 1;border-top: 1px dashed #000;height: 0;position: ~relative;top: 0;bottom: 0;margin: auto 1rem;'></span>" +
                "        <span>0單</span>" +
                "      </div><div style='display: flex;justify-content: space-between;'>" +
                "        <span>餐廳外賣</span>" +
                "        <span style='flex: 1;border-top: 1px dashed #000;height: 0;position: relative;top: 0;bottom: 0;margin: auto 1rem;'></span>" +
                "        <span>0單</span>" +
                "      </div><div style='display: flex;justify-content: space-between;'>" +
                "        <span>外賣平臺</span>" +
                "        <span style='flex: 1;border-top: 1px dashed #000;height: 0;position: relative;top: 0;bottom: 0;margin: auto 1rem;'></span>" +
                "        <span style='float:right'>0單</span>" +
                "      </div> </section></div>" +
                "</body>" +
                "</html>");


        logger.info("pdf is {}", file.getAbsolutePath());
        return file;
    }

    /**
     * 将HTML文件转成PDF文件
     *
     * @param htmlContent
     * @return
     * @throws Exception
     */
    public static File htmlToPdf(String htmlContent) throws Exception {
        htmlContent = htmlContent.replace("&", "&amp;");
        File pdfFile = MongoId.getInstance().generateTmpFile("pdf");
        File htmlFile = MongoId.getInstance().generateTmpFile("html");

        FileUtil.writeToFile(htmlFile, htmlContent);

        // step 1
        String url = htmlFile.toURI().toURL().toString();
        logger.info("html url {}", url);
        // step 2~
        OutputStream os = new FileOutputStream(pdfFile);
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocument(htmlFile);

        // step 3 解决中文支持
        ITextFontResolver fontResolver = renderer.getFontResolver();
        if ("linux".equals(getCurrentOperatingSystem())) {
            fontResolver.addFont("/runtime/dola/service/be/font/SimSun.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        } else {
            fontResolver.addFont("c:/Windows/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        }

        renderer.layout();
        renderer.createPDF(os);
        os.close();

        return pdfFile;
    }

    public static String getCurrentOperatingSystem() {
        String os = System.getProperty("os.name").toLowerCase();
        System.out.println("---------当前操作系统是-----------" + os);
        return os;
    }


}
